<html>
<head>
<link rel="shortcut icon" href="./favicon.ico">
<link rel="stylesheet" type="text/css" href="./style.css">
<link rel="canonical" href="./Demultiplexer_One_Hot.html">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Connects the `word_in` input port to one of the words in the `words_out` output port, as selected by the `selectors` one-hot vector, and raises the corresponding valid bit in the `valids_out` output port.  *If more than one bit is set in the `selectors` input, then all the corresponding output words will get the input word value.*">
<title>Demultiplexer One Hot</title>
</head>
<body>

<p class="inline bordered"><b><a href="./Demultiplexer_One_Hot.v">Source</a></b></p>
<p class="inline bordered"><b><a href="./legal.html">License</a></b></p>
<p class="inline bordered"><b><a href="./index.html">Index</a></b></p>

<h1>One-Hot Demultiplexer</h1>
<p>Connects the <code>word_in</code> input port to one of the words in the <code>words_out</code>
 output port, as selected by the <code>selectors</code> one-hot vector, and raises the
 corresponding valid bit in the <code>valids_out</code> output port.  <em>If more than one
 bit is set in the <code>selectors</code> input, then all the corresponding output
 words will get the input word value.</em></p>
<h2>Implementation Options</h2>
<p>Set the <code>BROADCAST</code> parameter to 1 to simply replicate and connect
 <code>word_in</code> to each word in <code>words_out</code>, without any logic. The valid bit
 will indicate which downstream logic should accept the data, and other
 logic can snoop the data if that's part of your larger design.</p>
<p>Set the <code>BROADCAST</code> parameter to 0 to send <code>word_in</code> to <em>only</em> the selected
 word in <code>words_out</code>, whose valid bit is set. All other output words are
 annulled to zero. If no valid bit is set, all output words stay at zero.</p>
<p>Not broadcasting the input to the output costs some logic, but less than it
 appears from a standalone synthesis of the demultiplexer: the inferred
 <a href="./Annuller.html">Annullers</a> will very likely disappear into downstream LUT
 logic, it also makes tracing a simulation easier, and adds some design
 security and robustness since unselected downstream logic cannot snoop or
 accidentally receive other data.</p>
<p>If necessary, select the Annuller <code>IMPLEMENTATION</code> which yields the best
 logic synthesis. Usually, this does not matter, and can be set to "AND".</p>
<p>Setting <code>BROADCAST</code> to any value other than 1 or 0 will disconnect
 <code>word_in</code> from <code>words_out</code>, raise some critical warnings in your CAD tool,
 and generally cause a lot of downstream logic to optimize away, so you
 should notice...</p>

<pre>
`default_nettype none

module <a href="./Demultiplexer_One_Hot.html">Demultiplexer_One_Hot</a>
#(
    parameter       BROADCAST           = 0,
    parameter       WORD_WIDTH          = 0,
    parameter       OUTPUT_COUNT        = 0,
    parameter       IMPLEMENTATION      = "AND",

    // Do not set at instantiation
    parameter   TOTAL_WIDTH = WORD_WIDTH * OUTPUT_COUNT
)
(
    input   wire    [OUTPUT_COUNT-1:0]  selectors,
    input   wire    [WORD_WIDTH-1:0]    word_in,
    output  reg     [TOTAL_WIDTH-1:0]   words_out,
    output  reg     [OUTPUT_COUNT-1:0]  valids_out
);

    localparam OUTPUT_ZERO = {OUTPUT_COUNT{1'b0}};
    localparam TOTAL_ZERO  = {TOTAL_WIDTH{1'b0}};

    initial begin
        words_out  = TOTAL_ZERO;
        valids_out = OUTPUT_ZERO;
    end
</pre>

<p>Pass along the selector to the downstream logic, so we know which output
 word is the selected one.</p>

<pre>
    always @(*) begin
        valids_out = selectors;
    end
</pre>

<p>If we are <em>not</em> broadcasting, then for each output word, annul the output
 if its selector bit is not set.  Thus, only the selected output word will
 have the <code>word_in</code> data. All others will stay at zero.  Otherwise, simply
 replicate and connect the input to all outputs. </p>

<pre>
    generate
        if (BROADCAST == 0) begin : gen_no_broadcast
            wire [TOTAL_WIDTH-1:0] words_out_internal;

            genvar i;
            for (i=0; i < OUTPUT_COUNT; i=i+1) begin: per_output
                <a href="./Annuller.html">Annuller</a>
                #(
                    .WORD_WIDTH     (WORD_WIDTH),
                    .IMPLEMENTATION (IMPLEMENTATION)
                )
                output_gate
                (
                    .annul          (selectors[i] == 1'b0),
                    .data_in        (word_in),
                    .data_out       (words_out_internal[WORD_WIDTH*i +: WORD_WIDTH])
                );
            end

            always @(*) begin
                words_out = words_out_internal;
            end
        end
        else
        if (BROADCAST == 1) begin : gen_broadcast
            always @(*) begin
                words_out = {OUTPUT_COUNT{word_in}};
            end
        end
    endgenerate

endmodule
</pre>

<hr>
<p><a href="./index.html">Back to FPGA Design Elements</a>
<center><a href="https://fpgacpu.ca/">fpgacpu.ca</a></center>
</body>
</html>

